﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cwtrock.model;
using cwtrock.dao.impl;
using cwtrock.service.impl;
using cwtrock.syscommon;

namespace cwtrock.view
{
    public partial class VoucherListMain : Form
    {
        private static VoucherListMain frm = null;

        private VoucherServiceImpl voucherService = new VoucherServiceImpl();
        private VrecordsServiceImpl vrecordsService = new VrecordsServiceImpl();
        private AccountsServiceImpl accountsService = new AccountsServiceImpl();
        private List<Vouchers> vouls;

        // 按条件获取的分录数据
        private List<Vrecords> vrls;
        private List<Accounts> accountls = new List<Accounts>();
        public static Vouchers voucherQuery;

        #region 单体模式
        /// <summary>
        /// 单体模式
        /// </summary>
        /// <returns></returns>
        public static VoucherListMain getInstance()
        {
            if (frm == null)
            {
                frm = new VoucherListMain();

            }

            return frm;
        }
        #endregion

        public VoucherListMain()
        {
            InitializeComponent();
            //这是了隔行换色的代码  
            this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.FromArgb(255, 255, 255);//第一行  
            this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(244, 247, 252);//第二行  
            this.dataGridView1.GridColor = Color.FromArgb(128, 128, 128);//字段网格边框  
            dataGridView1.Focus();

            // 去除表头排序
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }


            initGrid();
        }
       

        // 将查询结果显示到列表
        public void initGrid()
        {

            if (voucherQuery == null)
            {
                return;
            }
            // 按条件取得凭证
            vouls = voucherService.getVouchersByCondition(voucherQuery);
            // 按条件取得分录
            vrls = vrecordsService.getVrecordsByCondition(voucherQuery);
            if (vouls == null)
            {
                return;
            }

            dataGridView1.DataSource = getData(vouls);
            vcount_txt.Text = vouls.Count+"";
            dataGridView1.Select();
            
          
        }


        private DataTable getData(List<Vouchers> vouls)
        {
            // 全部的科目数据
            accountls = accountsService.getAcctAll();

            DataTable dt = new DataTable("Table_AX");

            DataColumn dc = new DataColumn("column1", System.Type.GetType("System.Boolean"));

            dt.Columns.Add("id", System.Type.GetType("System.String"));
            dt.Columns.Add("term", System.Type.GetType("System.String"));
            dt.Columns.Add("post", System.Type.GetType("System.String"));
            dt.Columns.Add("close", System.Type.GetType("System.String"));
            dt.Columns.Add("abondon", System.Type.GetType("System.String"));
            dt.Columns.Add("date", System.Type.GetType("System.String"));
            dt.Columns.Add("vtypeAndCode", System.Type.GetType("System.String"));
            dt.Columns.Add("digest", System.Type.GetType("System.String"));
            dt.Columns.Add("acctcode", System.Type.GetType("System.String"));
            dt.Columns.Add("acctname", System.Type.GetType("System.String"));
            dt.Columns.Add("amount", System.Type.GetType("System.String"));
            dt.Columns.Add("bamount", System.Type.GetType("System.String"));
            dt.Columns.Add("lamount", System.Type.GetType("System.String"));
            dt.Columns.Add("maker", System.Type.GetType("System.String"));
            dt.Columns.Add("checker", System.Type.GetType("System.String"));
            dt.Columns.Add("poster", System.Type.GetType("System.String"));

            
            foreach (Vouchers vou in vouls)
            {
               
                DateTime vdate = Convert.ToDateTime(vou.Vdatestr);
                
                // 设置凭证的分录集合
                List<Vrecords> rls = vrecordsService.getVrecordsByVouid(vrls,vou.Id);

                vou.vrecordsls = rls;

                if (rls == null)
                    break;
                string post_stat = getStates(vou.Vpoststat);
                string closed_stat = getStates(vou.Vclosedstat);
                string used_stat = getStates(vou.Vusedstat);
                string datestr = vdate.ToShortDateString();
                string vou_code = vou.Vtypename + "-" + vou.Vcode;

                int rowc = 0;

                foreach(Vrecords vr in rls){
                
                    DataRow dr = dt.NewRow();
                    dr["id"] = vou.Id;
                    dr["term"] = vdate.Month;
                    dr["post"] = post_stat;
                    dr["close"] = closed_stat;
                    dr["abondon"] = used_stat;

                    if (rowc == 0)
                    { 
                     
                        dr["date"] = datestr;
                        dr["vtypeAndCode"] = vou_code;
                    
                    }
              
                    dr["digest"] = vr.Vrdigest;

                    string acctcode= vr.Acctcode;  
                    string acctname = accountsService.getAcctname(accountls, acctcode);
                    vr.Acctname = acctname;
                    dr["acctcode"] = acctcode;
                    dr["acctname"] = acctname;
                    string amount = vr.Vrbamount == 0 ? vr.Vrlamount + ".00" : vr.Vrbamount + ".00";
                    dr["amount"] = "¥" + amount;
                    dr["bamount"] = vr.Vrbamount == 0 ? "" : "¥" + vr.Vrbamount + ".00";
                    dr["lamount"] = vr.Vrlamount == 0 ? "" : "¥" + vr.Vrlamount + ".00";
                    dr["maker"] = vou.Vmaker;
                    dr["checker"] = vou.Vchecker;
                    dr["poster"] = vou.Vposter;
                    dt.Rows.Add(dr);

                    rowc++;

                }
            }

            return dt;
        }



        // 若凭证已审核，已过账，已作废返回*，否则返回""
        private string getStates(string stat) { 
           if("2".Equals(stat)){
               return "*"; 
            
           }
           return "";
        }


        private void VoucherListMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm = null;
            VouQueryFrm.vmainfrm = null;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            string id = this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            Vouchers vou = voucherService.getVouchersByVouid(vouls, long.Parse(id));
            VoucherEditFrm editfrm = VoucherEditFrm.getInstance(vou, SysConst.FRM_FLAG_VIEW);
            editfrm.Show();
        }

        private void VoucherListMain_Load(object sender, EventArgs e)
        {
            vcount_txt.Undo();
            dataGridView1.Focus();
        //    dataGridView1.Rows[0].Selected = true;
        }

        private void edit_tbtn_Click(object sender, EventArgs e)
        {
            int row = dataGridView1.CurrentRow.Index;
            string id = this.dataGridView1.Rows[row].Cells[0].Value.ToString();
            Vouchers vou = voucherService.getVouchersByVouid(vouls, long.Parse(id));
            VoucherEditFrm editfrm = VoucherEditFrm.getInstance(vou, SysConst.FRM_FLAG_EDIT);
             editfrm.ShowDialog();
            
             // 刷新列表
             initGrid();
             dataGridView1.Rows[0].Selected = false;
             for (int i = 0; i < dataGridView1.Rows.Count; i++)
             {
                 string idx = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                 if (idx.Equals(id)) {
                     dataGridView1.Rows[i].Selected = true;
                     break;
                 
                 }
             
             }

        }

        private void save_tbtn_Click(object sender, EventArgs e)
        {
            VoucherFrm frm = VoucherFrm.getInstance();
            frm.ShowDialog();

            // 刷新列表
            initGrid();
            dataGridView1.Rows[0].Selected = false;
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
        }

        private void delete_tbtn_Click(object sender, EventArgs e)
        {
            int row = dataGridView1.CurrentRow.Index;
            string id = this.dataGridView1.Rows[row].Cells[0].Value.ToString();
            Vouchers vou = voucherService.getVouchersByVouid(vouls, long.Parse(id));
            DateTime vdate = Convert.ToDateTime(vou.Vdatestr);
            DialogResult dialogResult = MessageBox.Show("确定要删除\"第" + vdate.Month + "期" + vou.Vtypename + "字第" + vou.Vcode + "号\"凭证?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
             if (dialogResult == DialogResult.Yes)
             {
                 voucherService.delete(vou);
                 // 刷新列表
                 initGrid();
             }


        }

        private void refresh_tbtn_Click(object sender, EventArgs e)
        {
            // 刷新列表
            initGrid();
        }

        

    }
}
